import router from "@/router"
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import { getToken } from "@/utils/auth.ts"

NProgress.configure({ showSpinner: false })

// 配置不需要token 页面
const whiteList = ["/login"]
router.beforeEach(async (to) => {
  NProgress.start()
  const hasToken = getToken()
  if (hasToken) {
    // 用token 换取用户信息
    if (to.path === "/login") {
      NProgress.done()
      return {
        path: "/",
        replace: true
      }
    }
    NProgress.done()
  } else {
    if (whiteList.includes(to.path)) {
      NProgress.done()
      return true
    }

    NProgress.done()
    return {
      path: "/login",
      query: {
        redirect: to.path,
        ...to.query
      }
    }
  }
})
